<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css">
</head>
<style>
</style>
<body id="body" style="padding-bottom: 90px; background-color: #ECF0F5;">
	<div class="modal-content">
		<div class="modal-body">
			<div class="form-group">
				<label class="control-label col-sm-2">所属小区：</label> <input type="text" class="form-control" disabled="disabled" value="${sessionScope.communityName}">
			</div>
			<div class="form-group">
				<label class="control-label col-sm-2">上级节点：</label> <input type="text" class="form-control" id="pId" value="无" disabled="disabled">
			</div>
			<div class="form-group">
				<label class="control-label col-sm-2">标题：</label> <input type="text" class="form-control" id="content">
			</div>
			<!-- 			<div class="form-group"> -->
			<!-- 				<label class="control-label col-sm-2 checkbox-inline"> <input onclick="selectChange()" type="checkbox" id="isFinal">是否最终节点 -->
			<!-- 				</label> -->
			<!-- 			</div> -->
			<div id="category" class="form-group">
				<label class="control-label col-sm-2">工单类别(只在最终节点生效)：</label> <select id="categoryId" class="form-control">
					<option value="">请选择...</option>
				</select>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>

<script>
	var lock = false;
	var id = "${map['id']}";
	var pId = "${map['pId']}";
	var index = parent.layer.getFrameIndex(window.name);
	$(function() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectPropertyTypeByAll",
			type : "post",
			dataType : "json",
			success : function(map) {
				var list = map['list'];

				var html = "<option value=''>请选择...</option>";
				for (var i = 0; i < list.length; i++) {
					html += "<option value='"+list[i].id+"'>" + list[i].name + "</option>";
				}
				$("#categoryId").html(html);
				if (id != '') {
					$.ajax({
						url : "${pageContext.request.contextPath}/selectPropertyResourceById",
						type : "post",
						data : {
							id : id
						},
						dataType : "json",
						success : function(map) {
							var resource = map['resource'];
							$("#content").val(resource.content);
							// 							if (resource.isFinal == 1) {
							$("#categoryId").val(resource.categoryId);
							$("#isFinal").attr("checked", true);
							// 							}
							pId = resource.pId;
							selectByPId();
						}
					});
				} else {
					selectByPId();
				}
			}
		});
	});

	function selectByPId() {
		if (pId != 1) {
			$.ajax({
				url : "${pageContext.request.contextPath}/selectPropertyResourceByPId",
				type : "post",
				data : {
					pId : pId
				},
				dataType : "json",
				success : function(map) {
					if (map['resource'] != null) {
						$("#pId").val(map['resource'].content);
					}
				}
			});
		}
	}

	//添加或修改
	function insertPropertyResource() {
		lock = true;
		var content = $("#content").val();
		var isFinal = 0;
		// 		if ($('#isFinal').is(':checked')) {
		// 			isFinal = 1;
		// 		}
		var categoryId = $("#categoryId").val();
		if (content === '') {
			layer.alert("请填写标题！");
			lock = false;
			return;
		}
		if (categoryId == '') {
			layer.alert("请填写工单类型！");
			lock = false;
			return;
		}
		$.ajax({
			url : '${pageContext.request.contextPath}/insertPropertyResource',
			type : "post",
			data : {
				id : id,
				pId : pId,
				categoryId : categoryId,
				isFinal : isFinal,
				content : content
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						parent.query();
						parent.layer.close(index);
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
					lock = false;
				}
			}
		});
	}

	function insert() {
		if (!lock) {
			insertPropertyResource();
		}
	}

	// 	function selectChange() {
	// 		if ($('#isFinal').is(':checked')) {
	// 			$("#category").show();
	// 		} else {
	// 			$("#category").hide();
	// 			$("#categoryId").val('');
	// 		}
	// 	}
</script>
</html>
